<%-- 
    Document   : user-add
    Created on : Feb 9, 2012, 8:24:56 AM
    Author     : PhucAn
--%>

<%@page import="util.MailUtil"%>
<%@page import="util.SharingUtil"%>
<%@page import="java.util.Date"%>
<%@page import="util.DateTime"%>
<%@page import="util.MD5Encryption"%>
<%@page import="dto.User"%>
<%@page import="dao.UserDAO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
 
<div id="content-container">
    <form method="post" action="#">
        <table class="form-table">
            <tr>
                <td></td>
                <td colspan="2"><h1>Register</h1></td>
            </tr>
            <tr>
                <td>Username: </td>
                <td><input type="text" id="username" name="username"/></td>
                <td></td>
            </tr>
            <tr>
                <td>Password: </td>
                <td><input type="password" id="password" name="password"/></td>
                <td></td>
            </tr>
            <tr>
                <td>First name: </td>
                <td><input type="text" id="firstname" name="firstname"/></td>
                <td></td>
            </tr>
            <tr>
                <td>Last name: </td>
                <td><input type="text" id="lastname" name="lastname"/></td>
                <td></td>
            </tr>
            <tr>
                <td>Gender: </td>
                <td><input type="checkbox" id="gender" name="gender"/></td>
                <td></td>
            </tr>
            <tr>
                <td>Date of birth: </td>
                <td><input type="text" name="dob" id="dob"/></td>
                <td></td>
            </tr>
            <tr>
                <td>Email: </td>
                <td><input type="text" id="email" name="email"/></td>
                <td></td>
            </tr>                    
            <tr>
                <td></td>
                <td colspan="2">
                    <input type="submit" name="submit" value="Create"/>
                    <input type="reset" name="reset" value="Reset"/>
                </td>
            </tr>
        </table>
    </form>
</div>

<%
    MD5Encryption md5 = new MD5Encryption();
    if (request.getParameter("submit") == null) {
        return;
    }
    UserDAO daoUser = new UserDAO();
    User us = new User();
    us.setUsername(request.getParameter("username"));
    us.setPassword(md5.encrypt(request.getParameter("password")));
    us.setFirstName(request.getParameter("firstname"));
    us.setLastName(request.getParameter("lastname"));
    us.setGender(request.getParameter("gender").toLowerCase().equals("on"));
    us.setDateOfBirth(DateTime.parseToDate(request.getParameter("dob"), DateTime.DATE));
    us.setEmail(request.getParameter("email"));
    us.setRole(3);
    us.setRegisteredDate(new Date());
    if (daoUser.create(us) > 0) {
        SharingUtil.showMessage(request, "New user account is created");
        String smtpServer = application.getAttribute(SharingUtil.VAR_A_MAIL_SERVER).toString();
        String smtpPort = application.getAttribute(SharingUtil.VAR_A_MAIL_PORT).toString();
        String mailUser = application.getAttribute(SharingUtil.VAR_A_MAIL_USER_ID).toString();
        String mailPassword = application.getAttribute(SharingUtil.VAR_A_MAIL_PASSWORD).toString();
        String subject = "Online Quiz - User registration";
        String body = "A user account has been registered.<br/>";
        body += ("Email: " + us.getEmail());
        MailUtil mailer=new MailUtil();
        mailer.send(smtpServer, smtpPort, us.getEmail(), mailUser, mailPassword, subject, body);
        response.sendRedirect("index.jsp?pcontent=success");

    } else {
        SharingUtil.showMessage(request, "User account cannot be created. Action failed.");
    }
%>
